package ${model.packagePath}.${model.moduleName}.controller;

<#if model.extendsBaseDO>
import com.imyuanma.qingyun.common.factory.BaseDOBuilder;
</#if>
import com.imyuanma.qingyun.common.model.request.WebRequest;
import com.imyuanma.qingyun.common.model.response.Page;
import com.imyuanma.qingyun.common.model.PageQuery;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.AssertUtil;
import ${model.packagePath}.${model.moduleName}.model.${model.className};
import ${model.packagePath}.${model.moduleName}.service.I${model.className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${model.name!}web
 *
 * @author ${model.author!}
 * @date ${nowTimeStr}
 */
@RestController
@RequestMapping(value = "/${model.moduleName}/${model.className?uncap_first}")
public class ${model.className}Controller {

    /**
     * ${model.name!}服务
     */
    @Autowired
    private I${model.className}Service ${model.className?uncap_first}Service;

    /**
     * 列表查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/getList")
    public Result<${"List"}<${model.className}>> getList(@RequestBody WebRequest<${model.className}> webRequest) {
        return Result.success(${model.className?uncap_first}Service.getList(webRequest.getBody()));
    }

    /**
     * 分页查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/getPage")
    public Page<${"List"}<${model.className}>> getPage(@RequestBody WebRequest<${model.className}> webRequest) {
        PageQuery pageQuery = webRequest.buildPageQuery();
        List<${model.className}> list = ${model.className?uncap_first}Service.getPage(webRequest.getBody(), pageQuery);
        return Page.success(list, pageQuery);
    }

    /**
     * 统计数量
     *
     * @param webRequest 查询条件
     * @return
     */
    @PostMapping("/count")
    public Result<${"Integer"}> count(@RequestBody WebRequest<${model.className}> webRequest) {
        return Result.success(${model.className?uncap_first}Service.count(webRequest.getBody()));
    }

    /**
     * 查询
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/get")
    public Result<${model.className}> get(@RequestBody WebRequest<${primaryKeyJavaType}> webRequest) {
        AssertUtil.notNull(webRequest.getBody());
        return Result.success(${model.className?uncap_first}Service.get(webRequest.getBody()));
    }

    /**
     * 新增
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody WebRequest<${model.className}> webRequest) {
        ${model.className} ${model.className?uncap_first} = webRequest.getBody();
        AssertUtil.notNull(${model.className?uncap_first});
        <#if model.extendsBaseDO>
        BaseDOBuilder.fillBaseDOForInsert(${model.className?uncap_first});
        </#if>
        ${model.className?uncap_first}Service.insertSelective(${model.className?uncap_first});
        return Result.success();
    }

    /**
     * 修改
     *
     * @param webRequest 参数
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody WebRequest<${model.className}> webRequest) {
        ${model.className} ${model.className?uncap_first} = webRequest.getBody();
        AssertUtil.notNull(${model.className?uncap_first});
        AssertUtil.notNull(${model.className?uncap_first}.get${primaryKeyJavaName?cap_first}());
        <#if model.extendsBaseDO>
        BaseDOBuilder.fillBaseDOForUpdate(${model.className?uncap_first});
        </#if>
        ${model.className?uncap_first}Service.updateSelective(${model.className?uncap_first});
        return Result.success();
    }

    /**
     * 删除
     *
     * @param webRequest 参数, 待删除主键,多个使用英文逗号拼接
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody WebRequest<${"String"}> webRequest) {
        String ids = webRequest.getBody();
        AssertUtil.notBlank(ids);
        if (ids.contains(",")) {
        <#if primaryKeyJavaType == "String">
            ${model.className?uncap_first}Service.batchDelete(Arrays.stream(ids.split(",")).collect(Collectors.toList()));
        <#else>
            ${model.className?uncap_first}Service.batchDelete(Arrays.stream(ids.split(",")).map(${primaryKeyJavaType}::valueOf).collect(Collectors.toList()));
        </#if>
        } else {
        <#if primaryKeyJavaType == "String">
            ${model.className?uncap_first}Service.delete(ids);
        <#else>
            ${model.className?uncap_first}Service.delete(${primaryKeyJavaType}.valueOf(ids));
        </#if>
        }
        return Result.success();
    }

}
